<template>
  <div class="info">
    <template>
      <el-skeleton :loading="loading" />
    </template>
    <template v-if="!loading">
      <el-form :model="searchInfo" inline>
        <el-form-item>
          <el-input clearable v-model="searchInfo.name" placeholder="请输入角色名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getList">查询</el-button>
          <el-button icon="el-icon-refresh-right" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item>
          <el-button type="success" icon="el-icon-folder-add" @click="openWin()">添加</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="infoList" border>
        <el-table-column label="编号" prop="id"/>
        <el-table-column label="角色名" prop="name"/>
        <el-table-column label="授权码" prop="tips"/>
        <el-table-column label="部门名" prop="deptName"/>
        <el-table-column label="上级角色" prop="pName"></el-table-column>
        <el-table-column label="操作" width="150px">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="openWin(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="delInfo(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination layout="total,prev,pager,next,sizes,jumper"
                     :total="pageInfo.total"
                     :page-size="pageInfo.size"
                     :current-page="pageInfo.current"
                     :page-sizes="[5,10,20,30,50,100]"
                     @size-change="sizeChange"
                     @current-change="currentChange"
      />
    </template>
    <RoleEdit ref="roleEdit" @reloadTable="getList"/>
  </div>
</template>

<script>
import RoleEdit from './RoleEdit'
export default {
  name: 'Role',
  data () {
    return {
      loading: true,
      searchInfo: {
        name: '',
        page: 1,
        limit: 5
      },
      infoList: [],
      pageInfo: {}
    }
  },
  components: { RoleEdit },
  methods: {
    getList () {
      this.$http.get('role/list', this.searchInfo).then(({ data }) => {
        this.loading = false
        if (data.code === 20000) {
          this.infoList = data.data
        }
      })
    },
    sizeChange (limit) {
      this.searchInfo.limit = limit
      this.getList()
    },
    currentChange (page) {
      this.searchInfo.page = page
      this.getList()
    },
    openWin (info) {
      this.$refs.roleEdit.init(info)
    },
    delInfo (roleId) {
      this.$confirm('确定删除该数据吗？', '询问').then(() => {
        this.$axios.delete('role', { params: { roleId } }).then(({ data }) => {
          if (data.code === 20000) {
            this.$message({
              message: '删除成功',
              type: 'success',
              onClose: () => {
                this.getList()
              }
            })
          } else {
            this.$alert('删除失败！', { type: 'error' })
          }
        })
      })
    },
    resetForm () {
      this.searchInfo.name = ''
      this.getList()
    }
  },
  created () {
    this.$nextTick(() => {
      this.getList()
    })
  }
}
</script>

<style scoped>

</style>
